package cn.wolfcode.education.web.controller;

import cn.wolfcode.education.domain.SystemDictionary;
import cn.wolfcode.education.domain.SystemDictionaryItem;
import cn.wolfcode.education.service.ISystemDictionaryService;
import cn.wolfcode.education.util.JSONResult;
import cn.wolfcode.education.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionary")
public class SystemDictionaryController {

    @Autowired
    private ISystemDictionaryService systemDictionaryService;

    @RequestMapping("")
    public String getItemByParentId() {
        return "systemdictionary/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public PageResult list() {
        return systemDictionaryService.getSystemDictionary();
    }

    @RequestMapping("/selectAll")
    @ResponseBody
    public List<SystemDictionary> selectAll() {

        return systemDictionaryService.selectAll();
    }

    @RequestMapping("/itemlist")
    @ResponseBody
    public PageResult itemlist(Long parentId) {

        return systemDictionaryService.getSystemDictionaryItemToPageRestult(parentId);
    }

    @RequestMapping("/getItem")
    @ResponseBody
    public List<SystemDictionaryItem> getItem(Long parentId) {
        return systemDictionaryService.getSystemDictionaryItem(parentId);
    }

    @RequestMapping("/grade")
    @ResponseBody
    public List<SystemDictionaryItem> grade() {

        return systemDictionaryService.getSystemDictionaryItem(2L);
    }

    @RequestMapping("/shc")
    @ResponseBody
    public List<SystemDictionaryItem> shc() {
        return systemDictionaryService.getSystemDictionaryItem(1L);
    }

    /**
     * 性别:18男---19女
     *
     * @return
     */
    @RequestMapping("/gender")
    @ResponseBody
    public List<SystemDictionaryItem> gender() {
        return systemDictionaryService.getSystemDictionaryItem(5L);
    }

    @ResponseBody
    @RequestMapping("/college")
    public List<SystemDictionaryItem> college() {

        return systemDictionaryService.getSystemDictionaryItem(6L);
    }

    //=================================增删改=================================================
    @RequestMapping("/save")
    @ResponseBody
    public JSONResult save(SystemDictionary systemDictionary) {
        JSONResult jsonResult = new JSONResult();
        try {
            systemDictionaryService.save(systemDictionary);
        } catch (RuntimeException e) {
            e.printStackTrace();
            jsonResult.setMsg("操作失败,");
        }
        return jsonResult;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id) {
        JSONResult jsonResult = new JSONResult();
        try {
            systemDictionaryService.delete(id);
        } catch (RuntimeException e) {
            e.printStackTrace();
            jsonResult.setMsg("操作失败,");
        }
        return jsonResult;
    }

    @RequestMapping("/update")
    @ResponseBody
    public JSONResult update(SystemDictionary systemDictionary) {
        JSONResult jsonResult = new JSONResult();
        try {
            systemDictionaryService.update(systemDictionary);
        } catch (RuntimeException e) {
            e.printStackTrace();
            jsonResult.setMsg("操作失败,");
        }
        return jsonResult;
    }

    //=================================字典明细操作=========================================
    @RequestMapping("/saveitem")
    @ResponseBody
    public JSONResult saveitem(SystemDictionaryItem systemDictionaryItem) {
        JSONResult jsonResult = new JSONResult();
        try {
            systemDictionaryService.saveitem(systemDictionaryItem);
        } catch (RuntimeException e) {
            e.printStackTrace();
            jsonResult.setMsg("操作失败,");
        }
        return jsonResult;
    }

    @RequestMapping("/deleteitem")
    @ResponseBody
    public JSONResult deleteitem(Long id) {
        JSONResult jsonResult = new JSONResult();
        try {
            systemDictionaryService.deleteitem(id);
        } catch (RuntimeException e) {
            e.printStackTrace();
            jsonResult.setMsg("操作失败,");
        }
        return jsonResult;
    }

    @RequestMapping("/updateitme")
    @ResponseBody
    public JSONResult updateitme(SystemDictionaryItem systemDictionaryItem) {
        JSONResult jsonResult = new JSONResult();
        try {
            systemDictionaryService.updateitme(systemDictionaryItem);
        } catch (RuntimeException e) {
            e.printStackTrace();
            jsonResult.setMsg("操作失败,");
        }
        return jsonResult;
    }
}
